/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lucky9;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author nathaniel
 */
@WebServlet(name = "CommandServlet", urlPatterns = {"/command"})
public class CommandServlet extends HttpServlet {

  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();
    Player me = (Player) session.getAttribute("PLAYER");
    ServletContext context = request.getServletContext();
    Game game = (Game) context.getAttribute("GAME");
    String address = request.getRemoteAddr();
    String command = request.getParameter("command");

    try {
      if (game != null && me != null) {
        switch (command) {
          case "set-banker":
            String ip2 = request.getParameter("ip");
            if (ip2 != null && ip2.length() > 0 && me.isBanker()) {
              game.setBanker(game.getPlayerMap().get(ip2));
            }
            break;
          case "hitme":
            if (me.isTurn() && me.getIp().equals(address)) {
              game.turn(me, true);
              me.setStatus(Player.Status.HIT);
            }
            break;
          case "stand":
            if (me.isTurn() && me.getIp().equals(address)) {
              game.turn(me, false);
              me.setStatus(Player.Status.STAND);
            }
            break;
          case "bet":
            String bet = request.getParameter("bet");
            if (bet != null && bet.length() > 0) {
              if (game.getState() == Game.State.BETTING && me.getIp().equals(address)) {
                int betAmount = 0;
                try {
                  betAmount = Integer.parseInt(bet);
                } catch (Exception e) {
                }
                if (betAmount > 0) {
                  me.setStatus(Player.Status.READY);
                  me.setBet(betAmount);
                }
              }
            }
            break;
          case "start":
            if (me.isBanker() && me.getIp().equals(address)) {
              game.startBetting();
            }
            break;
        }
        out.print("done");
      }

    } catch (Exception e) {
      e.printStackTrace();
      out.print(e.toString());
    } finally {
      out.close();
    }
  }

  // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  /**
   * Handles the HTTP
   * <code>GET</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

  /**
   * Handles the HTTP
   * <code>POST</code> method.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
    processRequest(request, response);
  }

  /**
   * Returns a short description of the servlet.
   *
   * @return a String containing servlet description
   */
  @Override
  public String getServletInfo() {
    return "Short description";
  }// </editor-fold>
}
